//
//  CalendarScheduleViewController.m
//  FMS
//
//  Created by Steven on 10/11/13.
//  Copyright (c) 2013 QGSVN. All rights reserved.
//

#import "CalendarScheduleViewController.h"
#import "ScheduleCell.h"

#define WIDTH_COLUMN_ONE  200
#define WIDTH_COLUMN_TWO  320
#define WIDTH_COLUMN_THREE  180
#define WIDTH_COLUMN_FOUR  254
enum{
    ServiceType_Tag = 5,
    StartDate_Tag,
    EndDate_Tag,
    
};
enum ScheduleCellTag {
    Category_Program_Schedule_Tag = 5,
    Maintenance_Type_Schedule_Tag,
    Item_Type_Schedule_Tag,
    NextDone_Schedule_Tag
};
@interface CalendarScheduleViewController ()

@end

@implementation CalendarScheduleViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        dictLanguage = [ApplicationDelegate.arrayLanguage objectAtIndex:0];
    }
    return self;
}

- (void)listScheduleByMonth{
    
    [arraySchedule removeAllObjects];
    
    NSMutableArray *array;
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *role = [[userDefaults objectForKey:ACCOUNT_ROLE] lowercaseString];
    role = [role lowercaseString];
    if ([role isEqualToString:@"ma"] || [role isEqualToString:@"owner"] || [role isEqualToString:@"admin"]){
        array = [ScheduleBaseLite queryListsInDatabase:[CouchBaseLiteLib sharedCouchBaseLite].database];
    }
    else
        array = [ScheduleBaseLite queryByContractor:[userDefaults objectForKey:ACCOUNT_ID] ListsInDatabase:[CouchBaseLiteLib sharedCouchBaseLite].database];
    
    if ([array count] > 0) {
        for (NSMutableDictionary *dict in array) {
            
            NSString *strDateSchedue = [dict objectForKey:@"startDate"];
            if ([strDateSchedue length] < 10)
                continue;
            strDateSchedue = [strDateSchedue substringToIndex:10];
            NSDate *dateSchedule = [NSDate dateFormatDateFromString:strDateSchedue];
            NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
            [dateFormat setDateFormat:@"MMM"];
            if ([[dateFormat stringFromDate:[NSDate date]] isEqualToString:[dateFormat stringFromDate:dateSchedule]])
                [arraySchedule addObject:dict];
        }
    }
    [self.tbSchedule reloadData];

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self addTopBar];
    if (self.navBar)
    {
        self.navBar.navBarViewDelegate = self;
        self.navBar.rightButton.hidden = YES;
        
        self.navBar.titleLabel.text = [dictLanguage objectForKey:@"schedule.title-monthly-maintenance-schedule"];
    }
    
    arraySchedule = [[NSMutableArray alloc] init];
    [self listScheduleByMonth];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - NavViewDelegate

- (void)didClickedLeftButton{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark tableview datasource

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [arraySchedule count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellSchedule = @"ScheduleCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellSchedule];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellSchedule];
        
        UILabel *lblCategory = [[UILabel alloc] initWithFrame:CGRectMake(20, 22, WIDTH_COLUMN_ONE, 22)];
        lblCategory.tag = Category_Program_Schedule_Tag;
        lblCategory.font = [UIFont fontWithName:FMS_REGULAR_FONT size:15];
        lblCategory.textColor = FMS_COLOR_DEFAUL;
        [cell.contentView addSubview:lblCategory];
        
        UILabel *lblMaintenanceType = [[UILabel alloc] initWithFrame:CGRectMake(230, 22, WIDTH_COLUMN_TWO, 22)];
        lblMaintenanceType.tag = Maintenance_Type_Schedule_Tag;
        lblMaintenanceType.font = [UIFont fontWithName:FMS_REGULAR_FONT size:15];
        lblMaintenanceType.textColor = FMS_COLOR_DEFAUL;
        [cell.contentView addSubview:lblMaintenanceType];
        
        
        UILabel *lblItemType = [[UILabel alloc] initWithFrame:CGRectMake(560, 22, WIDTH_COLUMN_THREE, 22)];
        lblItemType.tag = Item_Type_Schedule_Tag;
        lblItemType.font = [UIFont fontWithName:FMS_REGULAR_FONT size:15];
        lblItemType.textColor = FMS_COLOR_DEFAUL;
        [cell.contentView addSubview:lblItemType];
        
        UILabel *lblNextDone = [[UILabel alloc] initWithFrame:CGRectMake(750, 22, WIDTH_COLUMN_FOUR, 22)];
        lblNextDone.tag = NextDone_Schedule_Tag;
        lblNextDone.font = [UIFont fontWithName:FMS_REGULAR_FONT size:15];
        lblNextDone.textColor = FMS_COLOR_DEFAUL;
        [cell.contentView addSubview:lblNextDone];
        
    }
    
    UILabel *lblCategory = (UILabel *)[cell.contentView viewWithTag:Category_Program_Schedule_Tag];
    UILabel *lblMaintenanceType = (UILabel *)[cell.contentView viewWithTag:Maintenance_Type_Schedule_Tag];
    
    UILabel *lblItemType = (UILabel *)[cell.contentView viewWithTag:Item_Type_Schedule_Tag];
    UILabel *lblNextDone = (UILabel *)[cell.contentView viewWithTag:NextDone_Schedule_Tag];
    
    NSMutableDictionary *scheduleDict = [arraySchedule objectAtIndex:indexPath.row];
    lblCategory.text = [scheduleDict objectForKey:@"scheduleCategory"];
    if ([[scheduleDict objectForKey:@"categoryId"] intValue] == Maintenance){
        lblMaintenanceType.text = [ApplicationDelegate getMaintenanceTypeName:[[scheduleDict objectForKey:@"maintenanceTypeId"] intValue]];
    }
    else
        lblMaintenanceType.text = @"";
    
    if ([[scheduleDict objectForKey:@"itemType"] intValue] == Building_Type) {
        lblItemType.text = [dictLanguage objectForKey:@"schedule.title-building"];
    }
    else
        lblItemType.text = [dictLanguage objectForKey:@"schedule.title-facility"];
    
    lblNextDone.text = [NSDate stringFormatTimeDateFromDate:[NSDate dateFormatDateFullFromString:[scheduleDict objectForKey:@"startDate"]]];
    
    
    return cell;
}

@end
